window.addEventListener('load', function() {
    //鼠标在图片上箭头显示，不在图片上箭头消失
    var focus = document.querySelector('.focus')
    var arrow_l = document.querySelector('.arrow-l')
    var arrow_r = document.querySelector('.arrow-r')
    focus.addEventListener('mouseenter', function() {
        arrow_l.style.display = 'block'
        arrow_r.style.display = 'block'
        clearInterval(timer)
        timer = null
    })

    focus.addEventListener('mouseleave', function() {
        arrow_l.style.display = 'none'
        arrow_r.style.display = 'none'
        timer = setInterval(function() {
            arrow_r.click()
        }, 2000)
    })

    //动态生成小圆圈
    var ul = document.querySelector('ul')
    var ol = document.querySelector('.circle')
    for (var i = 0; i < ul.children.length; i++) {
        var li = document.createElement('li')
        li.setAttribute('index', i)
        ol.appendChild(li)

        //小圆圈排他思想，绑定点击事件
        li.addEventListener('click', function() {
            for (var i = 0; i < ol.children.length; i++) {
                ol.children[i].className = ''
            }
            this.className = 'current'

            //点击小圆圈，移动图片
            var index = this.getAttribute('index')
            num = index
            circle = index
            animate(ul, -index * focus.offsetWidth)
        })
    }
    ol.children[0].className = 'current'

    //点击右侧按钮，图片滚动一张
    ul.appendChild(ul.children[0].cloneNode(true))
    var num = 0
    var circle = 0
    var flag = true
    arrow_r.addEventListener('click', function() {
        if (flag) {
            flag = false
            if (num == ul.children.length - 1) {
                ul.style.left = 0
                num = 0
            }
            num++
            animate(ul, -num * focus.offsetWidth, function() {
                flag = true
            })
            circle++
            if (circle == ol.children.length) {
                circle = 0
            }

            circleChange()
        }
    })

    arrow_l.addEventListener('click', function() {
        if (flag) {
            flag = false
            if (num == 0) {
                num = ul.children.length - 1
                ul.style.left = -num * focus.offsetWidth + 'px'

            }
            num--
            animate(ul, -num * focus.offsetWidth, function() {
                flag = true
            })
            circle--
            /* if (circle < 0) {
                    circle = ol.children.length - 1
                }  */
            circle = circle < 0 ? ol.children.length - 1 : circle
            circleChange()
        }

    })

    function circleChange() {
        for (var i = 0; i < ol.children.length; i++) {
            ol.children[i].className = ''
        }
        ol.children[circle].className = 'current'
    }

    //自动播放轮播图
    var timer = setInterval(function() {
        arrow_r.click()
    }, 2000)
})